//
//  AXFNetworkingManager.m
//  BEEQUICK
//
//  Created by 李典 on 16/11/20.
//  Copyright © 2016年 SevenGroup. All rights reserved.
//

#import "AXFNetworkingTools.h"
#import <AFHTTPSessionManager.h>

@implementation AXFNetworkingTools{
    AFHTTPSessionManager *_manager;
}


static id _instance;
+ (instancetype)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}
- (instancetype)init
{
    if (self = [super init]) {
//        NSURL *baseUrl = [NSURL URLWithString:@"http://iosapi.itcast.cn/loveBeen/"];
        _manager = [AFHTTPSessionManager manager];
        
        _manager.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    }
    return self;
}

- (void)getDataWithUrlStr:(NSString *)urlStr parameter:(NSDictionary *)parames complete:(void (^)(id, NSError *))completeBlock
{
    [_manager GET:urlStr parameters:parames progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completeBlock) {
            completeBlock(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completeBlock) {
            completeBlock(nil,error);
        }
    }];
}

- (void)getHomeDataComplete:(void (^)(id, NSError *))completeBlock
{
    NSString *urlStr = @"https://raw.githubusercontent.com/Ma7777777/test/master/Home.json";
    
    [self getDataWithUrlStr:urlStr parameter:nil complete:^(id responseOBJ, NSError *error) {
        if (completeBlock) {
            completeBlock(responseOBJ,error);
        }
    }];
}


// 左侧tableView  列表数据
- (void)getTableCellWithUrl:(NSString *)urlString withCallNum:(NSInteger )callNum  completeLeftCell:(void(^)(id responseOBJ, NSError *error))completeLeftCellBlock completeRightCell:(void(^)(id responseOBJ, NSError *error))completeRightCellBlock;
{
//   AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    NSDictionary *dict = @{@"call":@(callNum)};
    
    //http://iosapi.itcast.cn/loveBeen/supermarket.json.php
    [_manager POST:urlString parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        NSArray *arrayLT = responseObject[@"data"][@"categories"];
        if (completeLeftCellBlock) {
            completeLeftCellBlock(arrayLT,nil);
        }
        NSDictionary *dictRT = responseObject[@"data"][@"products"];
        completeRightCellBlock(dictRT,nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}


- (void)getRightTableDetailsDataWithShopID:(NSString *)shopID
{

    //        NSMutableArray *arrayModel = [[NSMutableArray alloc]init];
    //         self.modelList = [arrayRTM sortedArrayUsingComparator:^NSComparisonResult(AXFDetailsModel *obj1, AXFDetailsModel* obj2) {
    //             return  [obj2.product_id compare:obj1.product_id];
    ////             return  [obj1.name compare:obj2.name];
    //        }].copy;
}



@end
